package com.winit.schedule.task.tracker.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.matchers.GroupMatcher;

public class QuartzUtils {

    public static List<? extends Trigger> getAllTriggers(Scheduler scheduler) throws SchedulerException {
        List<Trigger> allTriggers = new ArrayList<Trigger>();
        List<String> groups = scheduler.getJobGroupNames();
        List<? extends Trigger> triggers = null;
        for (String group : groups) {
            GroupMatcher<JobKey> matcher = GroupMatcher.jobGroupEquals(group);
            Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
            for (JobKey jobKey : jobKeys) {
                triggers = scheduler.getTriggersOfJob(jobKey);
                if (null != triggers && !triggers.isEmpty()) {
                    allTriggers.addAll(triggers);
                }
            }
        }

        return allTriggers;
    }

    public static List<? extends Trigger> getAllTriggers(List<Scheduler> schedulers) throws SchedulerException {
        List<Trigger> allTriggers = new ArrayList<Trigger>();

        if (null != schedulers && schedulers.size() > 0) {
            List<? extends Trigger> triggers = null;
            for (Scheduler scheduler : schedulers) {
                triggers = getAllTriggers(scheduler);
                allTriggers.addAll(triggers);
            }
        }

        return allTriggers;
    }

    public static void pauseTrigger(Scheduler scheduler, String triggerName, String triggerGroup)
                                                                                                 throws SchedulerException {
        if (null != scheduler) {
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            scheduler.pauseTrigger(triggerKey);
        }
    }

    public static void resumeTrigger(Scheduler scheduler, String triggerName, String triggerGroup)
                                                                                                  throws SchedulerException {
        if (null != scheduler) {
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            scheduler.resumeTrigger(triggerKey);
        }
    }

    public static void rescheduleJob(Scheduler scheduler, String triggerName, String triggerGroup, String cronExpression)
                                                                                                                         throws SchedulerException {
        if (null != scheduler) {
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
            scheduler.rescheduleJob(triggerKey, trigger);
        }

    }

}
